<?php

//Pixel Gaming Pickup Bot

function pminfo($serverid, $players)
{
    /*$pid = pcntl_fork();
    if ($pid == -1) {
        die('could not fork');
    } else if ($pid) {
         // we are the parent
            echo "\nNew PMINFO INSTANCE STARTED!\n";
    } else {
         // we are the child
            include("pminfo.php");
    }*/
}

function stvwait($serverid, $playersblu, $playersred, $pickupid)
{
    $pid = pcntl_fork();
    if ($pid == -1) {
        die('could not fork');
    } else if ($pid) {
         // we are the parent
            echo "\nNew STVWAIT INSTANCE STARTED!\n";
    } else {
         // we are the child
            include("stvwait.php");
    }
}

function rand_str($length = 12, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890')
{
    // Length of character list
    $chars_length = (strlen($chars) - 1);

    // Start our string
    $string = $chars{rand(0, $chars_length)};
   
    // Generate random string
    for ($i = 1; $i < $length; $i = strlen($string))
    {
        // Grab a random character from our list
        $r = $chars{rand(0, $chars_length)};
       
        // Make sure the same two characters don't appear next to each other
        if ($r != $string{$i - 1}) $string .=  $r;
    }
   
    // Return the string
    return $string;
}

function find_map($userinput)
{
	global $pickupchannel;
	global $irc;

	//Set default return
	$returnvalue = "";

	switch($userinput)
	{
		case "":
			$returnvalue = "";
			break;

		case "bad": case "badl": case "badla": case "badlands": case "cp_badlands": case "cp_bad": case "faglands": case "lands": case "blands": case "b": case "dl": case "bl":
			$returnvalue = "cp_badlands";
			break;

		case "gra": case "gran": case "granary": case "grana": case "cp_gran": case "cp_granary": case "y":
			$returnvalue = "cp_granary";
			break;

		case "fre": Case "free": case "freight": case "cp_fr": case "cp_fre": case "cp_freight":
			$returnvalue = "cp_freight";
			break;

		case "obs": case "obscure": case "obsc": case "cp_obs": case "cp_obscure": case "cure": case "cp_lawlstickies": case "cp_obscure_final":
			$returnvalue = "cp_obscure_final";
			break;

		case "cold": case "coldfront": case "col": case "cfront": case "front": case "cp_col": case "cp_coldfront": case "cp_coldfront_rc3":
			$returnvalue = "cp_coldfront";
			break;

		case "cp_freight_final1": case "freight_final": case "freight_final1": case "newfre": case "newfreight": case "newfree": case "final1":
			$returnvalue = "cp_freight_final1";
			break;

		case "gravel": case "grav": case "gravelpit": case "gpit": case "cp_grav": case "cp_gravel": case "cp_gravelpit":
			$returnvalue = "cp_gravelpit";
			break;

		case "gor": case "gorge": case "cp_gorge": case "cp_gor":
			$returnvalue = "cp_gorge";
			break;

		case "gul": case "gull": case "gully": case "gullywash": case "cp_gul": case "cp_gull": case "cp_gully": case "cp_gullywash":
			$returnvalue = "cp_gullywash_imp3";
			break;

		case "foll": case "fol": case "follower": case "cp_fol": case "cp_foll": case "cp_follower":
			$returnvalue = "cp_follower";
			break;

		case "waste": case "was": case "cp_waste": case "cp_was": case "waste_v2": case "cp_waste_v2":
			$returnvalue = "cp_waste_v2";
			break;
			
		case "well": case "cp_well": case "foster": case "cp_stalemate":
			$returnvalue = "cp_well";
			break;

		case "via": case "viad": case "viaduct": case "duct": case "koth_via": case "koth": case "koth_viaduct":
			$returnvalue = "koth_viaduct";
			break;

		case "yuk": case "yukon": case "cp_yukon": case "yu": case "cp_yukon_final":
			$returnvalue = "cp_yukon_final";
			break;
			
		case "tur": case "turd": case "turdbine": case "turbine": case "fuckinggayctfthing": case "ctf_turbine": case "bine": case "ine": case "turb":
			$returnvalue = "ctf_turbine";
			break;

                case "cp_collis": case "cp_collis_b2": case "collis": case "coll": case "cp_coll": case "followerbutfun":
                        $returnvalue = "cp_collis_b2";
                        break;
			
		//Randomly pick
		case "granlands":
			$output = rand(1, 100);
			if($output > 50)
			{
				$returnvalue = "cp_badlands";
			} else
			{
				$returnvalue = "cp_granary";
			}
			break;

		default:
			$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00Unknown map: \"".$userinput."\"");
			break;
	}

	return $returnvalue;
}

function is_odd($number) 
{
	return $number & 1; // 0 = even, 1 = odd
}

function add_player($qauth, $nick, $class, $skill, $map)
{
	global $irc;
	global $pickup;
	global $pickupchannel;
        global $messageflood;

	//Get the actual map
	$map = find_map($map);

        //Check if the person is inside the wait period
        if($messageflood['addwait'][$qauth] < time())
        {
            //Check if the player isn't already added as that class
            if($pickup->is_added($qauth) == $class)
            {
                if($messageflood['alreadyadded'][$qauth] < time())
                {
                    $irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00Sorry ".$nick.", you are already added as that class");
                    $messageflood['alreadyadded'][$qauth] = time() + 45;
                }
            } else
            {
                    $counter3 = 0;
                    while($counter3 < 12)
                    {
                            if($pickup->player[$counter3]->qauth == $qauth)
                            {
                                    if($map == "")
                                    {
                                            $map = $pickup->player[$counter3]->map;
                                    }
                            }
                            $counter3++;
                    }

                    //Check if player is already added as another class so we can remove them
                    if($pickup->is_added($qauth) !== false)
                    {
                            $counter3 = 0;
                            while($counter3 < 12)
                            {
                                    if($pickup->player[$counter3]->qauth == $qauth)
                                    {
                                            if($map == "")
                                            {
                                                    $map = $pickup->player[$counter3]->map;
                                            }
                                    }
                                    $counter3++;
                            }

                            $pickup->rm_player($qauth);
                    }

                    //Add the player
                    $pickup->add_player($qauth, $nick, $class, $skill);

                    //Perform map vote if specified
                    if($map != "")
                    {
                            $pickup->mapvote($qauth, $map);
                    }

                    //Set add wait time
                    $messageflood['addwait'][$qauth] = time() + 60;
                    $messageflood['addwait2'][$qauth] = 0;

                    //Update channel topic
                    SetTopic();
            }
      } else
      {
          if($messageflood['addwait2'][$qauth] == 0)
          {
                $irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00".$nick.": You cannot re-add or change class less than 60 seconds after you have added");
                $messageflood['addwait2'][$qauth] = 1;
          }
      }
}

function rm_player($qauth)
{
	global $irc;
	global $pickup;
	global $pickupchannel;

	//Check if they are added						
	if($pickup->is_added($qauth) !== false)
	{
		$pickup->rm_player($qauth);
		//Update channel topic
                SetTopic();
	}
}

///////////////////////////////////////////////////
//MySQL functions  ///////////////////////////////
/////////////////////////////////////////////////

function mysql_escape($input)
{
	//Escape characters that could be used for SQL injection
	$input = str_replace("\\", "\\\\", $input);
	$input = str_replace(";", "\\;", $input);
	$input = str_replace("'", "\\'", $input);
	$input = str_replace("\"", "\\\"", $input);
	$input = str_replace("`", "\\`", $input);
	return $input;
}

function mysql_pu_lastpickup()
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`pickups` ORDER BY id DESC LIMIT 1;";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);
	
	//Get results
	$returnvalue = mysql_fetch_assoc($result);
	return $returnvalue;
}

function mysql_pu_adduser($qauth, $nick, $inviter, $skill_scout="3" , $skill_demoman="3" , $skill_soldier="3", $skill_medic="3")
{
	global $mysql;

	//Build MySQL query
	$sql = "INSERT INTO `".mysql_escape($mysql->db)."`.`users` (`id`, `qauth`, `nick`, `banned`, `admin`, `skill_scout`, `skill_demoman`, `skill_soldier`, `skill_medic`, `invitedby`, `banreason`, `invitedate`, `banexpire`, `played`) VALUES (NULL, '".mysql_escape($qauth)."', '".mysql_escape($nick)."', '0', '0', '".$skill_scout."', '".$skill_demoman."', '".$skill_soldier."', '".$skill_medic."', '".mysql_escape($inviter)."', '', '".time()."', '', '0');";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_addpickup($players, $classes, $map, $server)
{
	global $mysql;

	//Build MySQL query
	$sql = "INSERT INTO `".mysql_escape($mysql->db)."`.`pickups` (`id`, `players`, `classes`, `map`, `server`, `time`) VALUES (NULL, '".mysql_escape($players)."', '".mysql_escape($classes)."', '".mysql_escape($map)."', '".mysql_escape($server)."', UNIX_TIMESTAMP());";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

//Check if a user exists in the database
function mysql_pu_userexists($qauth)
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT `qauth` FROM `".mysql_escape($mysql->db)."`.`users` WHERE qauth='".mysql_escape($qauth)."'";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);

	//Examine
	$result = mysql_fetch_assoc($result);
	if($result["qauth"] == $qauth)
	{
		$returnvalue = true;
	} else
	{
		$returnvalue = false;
	}

	//Return the value
	return $returnvalue;
}

//Get info on a user
function mysql_pu_userinfo($qauth)
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`users` WHERE qauth='".mysql_escape($auth)."'";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);

	//Take to array and return value
	$result = mysql_fetch_assoc($result);

	return $result;
}

function mysql_pu_addmessage($command, $message)
{
	global $mysql;

	//Build MySQL query
	$sql = "INSERT INTO `".mysql_escape($mysql->db)."`.`messages` (`command`, `message`) VALUES ('".mysql_escape($command)."', '".mysql_escape($message)."');";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_updatemessage($command, $message)
{
	global $mysql;

	//Build SQL query
	$sql = "UPDATE `".mysql_escape($mysql->db)."`.`messages` SET message='".mysql_escape($message)."' WHERE command='".mysql_escape($command)."';";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_getmessages()
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`messages`";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);

	//take the query to array and return it
	while(($returnvalue[] = mysql_fetch_assoc($result)) || array_pop($returnvalue));

	return $returnvalue;
}

function mysql_pu_banuser($qauth, $expires, $admin, $length, $reason)
{
	global $mysql;
	
	//Build MySQL query
	$sql = "INSERT INTO `".mysql_escape($mysql->db)."`.`bans` (`id`, `qauth`, `reason`, `expires`, `admin`, `length`, `date`) VALUES (NULL, '".mysql_escape($qauth)."', '".mysql_escape($reason)."', '".mysql_escape($expires)."', '".mysql_escape($admin)."', '".mysql_escape($length)."', UNIX_TIMESTAMP());";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_unbanuser($qauth)
{
	global $mysql;
	
	//Build the MySQL query
	$sql = "UPDATE `".mysql_escape($mysql->db)."`.`bans` SET `expires` = UNIX_TIMESTAMP() WHERE `bans`.`qauth` = \"".mysql_escape($qauth)."\"";
	
	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_user_bans($qauth)
{
	global $mysql;
	
	//Build the SQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`bans` WHERE `bans`.`qauth` = \"".mysql_escape($qauth)."\"";
	
	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);

	//Get results from query
	while(($returnvalue[] = mysql_fetch_assoc($result)) || array_pop($returnvalue));
	
	//Return the results
	return $returnvalue;
}

function mysql_pu_addwarn($qauth, $reason, $admin)
{
	global $mysql;

	//Build MySQL query
	$sql = "INSERT INTO `".mysql_escape($mysql->db)."`.`warns` (`id`, `qauth`, `reason`, `admin`, `date`) VALUES (NULL, '".mysql_escape($qauth)."', '".mysql_escape($reason)."', '".mysql_escape($admin)."', UNIX_TIMESTAMP());";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_getwarns($qauth)
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`warns` WHERE `warns`.`qauth` = '".mysql_escape($qauth)."'";
	
	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);
	
	//Get results from query
	while(($returnvalue[] = mysql_fetch_assoc($result)) || array_pop($returnvalue));
	
	//Return the results
	return $returnvalue;
}

function mysql_pu_getbans($qauth)
{
	global $mysql;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`warns` WHERE `bans`.`qauth` = '".mysql_escape($qauth)."'";
	
	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);
	
	//Get results from query
	while(($returnvalue[] = mysql_fetch_assoc($result)) || array_pop($returnvalue));
	
	//Return the results
	return $returnvalue;
}

function mysql_pu_delmessage($command)
{
	global $mysql;

	//Build MySQL query
	$sql = "DELETE FROM `".mysql_escape($mysql->db)."`.`messages` WHERE `messages`.`command` = '".mysql_escape($command)."' LIMIT 1";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_userlist()
{
	global $mysql;
	global $users;

	//Build MySQL query
	$sql = "SELECT * FROM `".mysql_escape($mysql->db)."`.`users`";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	$result = mysql_query($sql, $link);
	mysql_close($link);

	//take the query to array and return it
	while(($returnvalue[] = mysql_fetch_assoc($result)) || array_pop($returnvalue));

	return $returnvalue;
}

function mysql_pu_nickupdate($qauth, $new_nick)
{
	global $mysql;
	global $users;

	//Build SQL query
	$sql = "UPDATE `".mysql_escape($mysql->db)."`.`users` SET `nick` = '".mysql_escape($new_nick)."' WHERE `users`.`qauth` = '".mysql_escape($qauth)."' LIMIT 1;";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

function mysql_pu_banupdate($nick)
{
	global $mysql;
	global $users;

	//Build SQL query
	$sql = "UPDATE `".mysql_escape($mysql->db)."`.`users` SET `banned` = '1' AND SET `banreason` = '".mysql_escape($banreason)."' WHERE `users`.`nick` = '".mysql_escape($nick)."' LIMIT 1;";

	//Execute the query
	$link = mysql_connect($mysql->host, $mysql->user, $mysql->pass);
	mysql_query($sql, $link);
	mysql_close($link);
}

//Initiated when 12 players are added
function start_pickup_s1()
{
    global $irc;
    global $users;
    global $pickup;
    global $afk;
    global $pickupchannel;
    global $waitbot;

    if($pickup->player_count() == 12)
    {
        //Clear the afk list
        $afk->clear();

            //Get a list of users
            $userlist = array();
            $counter = 0;
            while($counter < 12)
            {
                    $userlist[$counter] = $pickup->player[$counter]->qauth;
                    $counter++;
            }

            print_r($userlist);

            //Check if any users are afk
            $counter1 = 0;
            while($counter1 < 12)
            {
                    $timetest = time() - $users->last_active($userlist[$counter1]);
                    echo "\n".$timetest."\n";
                    if($timetest > 600)
                    {
                            $afk->add_player($userlist[$counter1]);
                    }
                    $counter1++;
            }

            if(count($afk->afklist) > 0)
            {
                    $count2 = count($afk->afklist);
                    $counter2 = 0;

                    //Generate the afk message
                    $afkmessage = "\x02\x0302,00The following players are currently marked as afk: \x02\x035,00";
                    while($counter2 < $count2)
                    {
                            $afkmessage = $afkmessage . $users->qauthtonick($afk->afklist[$counter2])." ";
                            $counter2++;
                    }

                    //Send the message to channel
                    $irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, $afkmessage);
                    $irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00These players have 60 seconds to type !ready before they are removed from the pickup.");

                    $waitbot->StartWait(60, 1);
            } else
            {
                    start_pickup_s2();
            }
     }
}

//Initiated when wait bot replys
function start_pickup_s2()
{
	global $pickup;
	global $users;
	global $pickupchannel;
	global $waitbot;
	global $irc;

	//Highlight the players
	$warning = "\x02\x0302,00Pickup Starting: \x02";

	$counter = 0;
	while($counter < 12)
	{
		$warning .= $users->qauthtonick($pickup->player[$counter]->qauth)." ";
		$counter++;
	}

	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, $warning);
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00Type !del if you can't play");

	//Start 20sec waitbot
	$waitbot->StartWait("15", 2);
}

function start_pickup_s3()
{
	global $pickup;
	global $users;
	global $pickupchannel;
	global $pickup;
	global $waitbot;
	global $servers;
	global $irc;

	//FUCK IT
	//LETS JUST CHOOSE ALL PLAYERS RANDOMLY

	//Generate player list
	$playerlist['scout'][0] = $pickup->classid_qauth("scout", 0);
	$playerlist['scout'][1] = $pickup->classid_qauth("scout", 1);
	$playerlist['scout'][2] = $pickup->classid_qauth("scout", 2);
	$playerlist['scout'][3] = $pickup->classid_qauth("scout", 3);

	$playerlist['soldier'][0] = $pickup->classid_qauth("soldier", 0);
	$playerlist['soldier'][1] = $pickup->classid_qauth("soldier", 1);
	$playerlist['soldier'][2] = $pickup->classid_qauth("soldier", 2);
	$playerlist['soldier'][3] = $pickup->classid_qauth("soldier", 3);

	$playerlist['demoman'][0] = $pickup->classid_qauth("demoman", 0);
	$playerlist['demoman'][1] = $pickup->classid_qauth("demoman", 1);

	$playerlist['medic'][0] = $pickup->classid_qauth("medic", 0);
	$playerlist['medic'][1] = $pickup->classid_qauth("medic", 1);

	print_r($pickup->player);
	print_r($playerlist);

	//Choose the teams
	$teams = choose_teams_random($playerlist);
	$blu = $teams['blu'];
	$red = $teams['red'];

	//WOOT WE FINALLY FINISHED ALL THE FUCKING BOLLOCKS
	//HONESTLY, IF YOU'RE READING THIS, WAEBI, DOTFLOAT, ETC, GO FUCK YOURSELF :DDD*/

	//Now find the winning map
	$pickup->build_votes();
	$counter = 0;
	$count = count($pickup->votes);
	$map['votes'] = 0;
	$map['map'] = "";

	while($counter < $count)
	{
		if($map['votes'] < $pickup->votes[$counter]["votes"])
		{
			$map['votes'] = $pickup->votes[$counter]["votes"];
			$map['map'] = $pickup->votes[$counter]["map"];
		}
		$counter++;
	}

	if($map['map'] == "")
	{
		$mapmessage = "Nobody voted for a map, so you get play something interesting: ";
		$winningmap = "cp_waste_v2";
	} else
	{
		$mapmessage = "";
		$winningmap = $map['map'];
	}

	//I'D LIKE TO THANK NO ONE AS EVERYONES ADVICE WAS COMPLETELY USELESS IN EVERY POSSIBLE WAY
	//FUCK YOU ALL
	//LOVE, ADAM

	//Find an empty server and change the map
	$counter = 1;
	$count = count($servers);
	while($counter <= $count)
	{
		if($servers[$counter]->is_empty() == true)
		{
			$serverid = $counter;
			$servers[$serverid]->changelevel($winningmap);
			$counter = $count + 1;
		}
		$counter++;
	}

        //Set channel +m to prevent spam
        $irc->mode($pickupchannel, "+m", SMARTIRC_CRITICAL);
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0302,00Pickup is now starting!");
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0312,00BLU: \x02Medic(".$users->qauthtonick($blu['medic']).") Soldiers(".$users->qauthtonick($blu['soldier'][0]).", ".$users->qauthtonick($blu['soldier'][1])." ) Demo(".$users->qauthtonick($blu['demoman'])." ) Scouts(".$users->qauthtonick($blu['scout'][0]).", ".$users->qauthtonick($blu['scout'][1])." )");
        $irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0304,00RED: \x02Medic(".$users->qauthtonick($red['medic']).") Soldiers(".$users->qauthtonick($red['soldier'][0]).", ".$users->qauthtonick($red['soldier'][1])." ) Demo(".$users->qauthtonick($red['demoman'])." ) Scouts(".$users->qauthtonick($red['scout'][0]).", ".$users->qauthtonick($red['scout'][1])." )");
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0302,00Map:\x02 ". $mapmessage. $winningmap);
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0302,00Server ".$serverid.":\x02 \x03".$servers[$serverid]->connect);
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0302,00New NL-based Servers, Remember to update your favourites list!");
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x02\x0302,00ALL PLAYERS MUST JOIN MUMBLE! Type !mumble for the mumble server info");
        $irc->Message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, "\x0302,00Please wait while the bot PM's all players the connect info");

        //Now PM all players with connect info
            $playerlist = $users->qauthtonick($blu['medic'])." ";
            $playerlist .= $users->qauthtonick($red['medic'])." ";
            $playerlist .= $users->qauthtonick($blu['soldier'][0])." ";
            $playerlist .= $users->qauthtonick($blu['soldier'][1])." ";
            $playerlist .= $users->qauthtonick($red['soldier'][0])." ";
            $playerlist .= $users->qauthtonick($red['soldier'][1])." ";
            $playerlist .= $users->qauthtonick($red['scout'][0])." ";
            $playerlist .= $users->qauthtonick($red['scout'][1])." ";
            $playerlist .= $users->qauthtonick($blu['scout'][0])." ";
            $playerlist .= $users->qauthtonick($blu['scout'][1])." ";
            $playerlist .= $users->qauthtonick($blu['demo'])." ";
            $playerlist .= $users->qauthtonick($red['demo']);

        pminfo($serverid, $playerlist);
        //system("screen -A -m -d -S pminfo php pminfo.php '".$playerlist."' ".$serverid);

	//Record info about this pickup to MySQL
	$counter = 0;
	while($counter < 12)
	{
		$players[$counter] = $pickup->player[$counter]->qauth;
		$classes[$counter] = $pickup->player[$counter]->class;
		$counter++;
	}

	$players = implode(" ", $players);
	$classes = implode(" ", $classes);

	mysql_pu_addpickup($players, $classes, $winningmap, $serverid);

	//Increment each users played Pickups counter
	

	//Reset to defaults for next pickup
	$pickup->init();
	
	//Get Pickup ID
	$thispickup = mysql_pu_lastpickup();
	$pickupid = $thispickup["id"];
	
	//Generate the players message to be sent to the channel
	$playersred = "RED: Medic(".$users->qauthtonick($red['medic']).") Soldiers(".$users->qauthtonick($red['soldier'][0]).", ".$users->qauthtonick($red['soldier'][1])." ) Demo(".$users->qauthtonick($red['demoman'])." ) Scouts(".$users->qauthtonick($red['scout'][0]).", ".$users->qauthtonick($red['scout'][1])." )";
	$playersblu = "BLU: Medic(".$users->qauthtonick($blu['medic']).") Soldiers(".$users->qauthtonick($blu['soldier'][0]).", ".$users->qauthtonick($blu['soldier'][1])." ) Demo(".$users->qauthtonick($blu['demoman'])." ) Scouts(".$users->qauthtonick($blu['scout'][0]).", ".$users->qauthtonick($blu['scout'][1])." )";
	
	//Start the 45min STV kill bot
        stvwait($serverid, $playersblu, $playersred, $pickupid);
	//system("screen -A -m -d -S STVServer-".$serverid." php stvwait.php ".$serverid. " ".$pickupid." '".$playersred."' '".$playersblu."'");
	
        SetTopic();

	$irc->mode($pickupchannel, "-m");
}

function rupmessage()
{
	global $afk;
	global $irc;
	global $users;
	global $pickupchannel;
	
	//Send message to channel
	$message = "\x0302,00The following players need to type !ready or will be removed from this pickup: \x0304,00";
	
	//Put list on message
	$counter = 0;
	$count = count($afk->afklist);
	while($counter < $count)
	{
		$message = $message . $users->qauthtonick($afk->afklist[$counter]) . " ";
		$counter++;
	}
	
	//Send message to the channel
	$irc->message(SMARTIRC_TYPE_CHANNEL, $pickupchannel, $message);
}

function choose_teams_random($playerlist)
{
	//Choose medics
	$medicn = rand(1, 100);
	if($medicn > 50)
	{
		$red['medic'] = $playerlist['medic'][0];
		$blu['medic'] = $playerlist['medic'][1];
	} else
	{
		$red['medic'] = $playerlist['medic'][1];
		$blu['medic'] = $playerlist['medic'][0];
	}

	//Choose demos
	$demon = rand(1,100);
	if($demon > 50)
	{
		$red['demoman'] = $playerlist['demoman'][0];
		$blu['demoman'] = $playerlist['demoman'][1];
	} else
	{
		$red['demoman'] = $playerlist['demoman'][1];
		$blu['demoman'] = $playerlist['demoman'][0];
	}

	//Choose scouts
		//Shuffle the scout array
		$scouts = $playerlist['scout'];
		shuffle($scouts);

		//Assign to teams
		$red['scout'][0] = $scouts[0];
		$red['scout'][1] = $scouts[1];
		$blu['scout'][0] = $scouts[2];
		$blu['scout'][1] = $scouts[3];

	//Choose Soldiers
		//Shuffle the soldier array
		$soldiers = $playerlist['soldier'];
		shuffle($soldiers);

		//Assign to teams
		$red['soldier'][0] = $soldiers[0];
		$red['soldier'][1] = $soldiers[1];
		$blu['soldier'][0] = $soldiers[2];
		$blu['soldier'][1] = $soldiers[3];

	//Get final value to return
	$returnvalue['red'] = $red;
	$returnvalue['blu'] = $blu;

	//And return the value
	return $returnvalue;
}

function TopicName($qauth)
{
    global $users;
    if($qauth == "")
    {
        $returnvalue = "x";
    } else
    {
        $returnvalue = $users->qauthtonick($qauth);
    }

    //Return final value
    return $returnvalue;
}

function SetTopic()
{
    global $statusinterp;
    global $pickup;
    global $irc;
    global $pickupchannel;
    global $TopicBot;
    global $eventstack;

    //Check the eventstack
    if($eventstack->CheckEvent() == true)
    {
        //Check interp setting
        if($statusinterp == 0)
        {
            $TopicBot->SetTopic($pickup->title());
            $statusinterp = 1;
        } else
        {
            $TopicBot->SetTopic("\x02\x02".$pickup->title());
            $statusinterp = 0;
        }
    }
}

?>
